package com.gxa.admin.controller;





import com.gxa.admin.service.AdminService;
import com.gxa.o2o.core.exception.BusinessException;

import com.gxa.o2o.core.pojo.Admin;
import com.gxa.o2o.core.utils.JsonResult;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {


    @Resource
    AdminService adminService;

    @Resource
    JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    JmsTemplate jmsTemplate;


    @PostMapping("/login")
    public JsonResult login(@RequestBody Admin admin) throws BusinessException {
        String phone=admin.getPhone();
        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
            throw new BusinessException("手机号码格式不合法");
        }
        JsonResult jsonResult=new JsonResult();
        Admin login = adminService.login(admin);
        if (login==null){
            jsonResult.setCode("0");
            jsonResult.setMsg("登录失败");
        }else {
            jsonResult.setMsg("登录成功");
            jsonResult.setCode("1");
            jsonResult.setData(login);
        }

        return jsonResult;
    }

    @RequestMapping(value = "/loginAndCode")
    public JsonResult login(@RequestParam("phone") String phone, @RequestParam("code") String code) throws BusinessException {
        if(!phone.matches("^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$")){
            throw new BusinessException("手机号码格式不合法");
        }
        JsonResult jsonResult=new JsonResult();
        Map<String, String> login = adminService.login(phone, code);
        jsonResult.setValue("token",login.get("token"));

        return  jsonResult;
    }

//    @PostMapping("/update")
//    public JsonResult itemDetail(Goods goods) throws BusinessException, URISyntaxException {
//        Integer lines = goodsService.updateById(goods);
//        JsonResult jsonResult = new JsonResult();
//        if (lines > 0) {
//            jsonResult.setCode("1");
//            jsonResult.setMsg("修改成功");
//        } else {
//            throw new BusinessException("修改失败,请检查商品编号是否真正确");
//        }

//        给前台portal发送一个远程调用请求,根据id删除缓存
//        RestTemplate restTemplate = new RestTemplate();
//        URI uri = new URI("http://localhost:8080/goods/delete");
//        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
//        paramMap.add("id",goods.getId());
//        String result = restTemplate.postForObject(uri, paramMap, String.class);
//        System.out.println("result"+result);
        //把要删除的商品的缓存的id发送到消息队列上,存在一个叫goodCache 的队列
//        jmsTemplate.convertAndSend("goodsCache",goods.getId());
//
//        return jsonResult;
//    }
//
//
//    @PostMapping("/selectAll")
//    public JsonResult selectAll() {
//        List<Goods> goodslist = goodsService.selectAll();
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.setCode("1");
//        jsonResult.setData(goodslist);
//        return jsonResult;
//    }
}
